import sys

outFile = open(sys.argv[1], 'w')
key = sys.argv[2]
numbers = []
for char in key:
  numbers.append(int(char))

inputSplits = []
for num in numbers :
  f = open(sys.argv[1] + ".split-" + str(num), "r", encoding="utf-8")
  inputSplits.append(f);

count = len(numbers)

finished = False
while not finished:
  lines = []
  for idx in range(count):
    splitFile = inputSplits[idx]
    sublineStr = splitFile.readline();
    if not sublineStr :
      finished = True
      lines.append("")
    else:
      lines.append(sublineStr[:-1])

  lineFinished = False;

  lineStr = ""

  while not lineFinished:
    for idx in range(count):
      if(len(lines[idx]) > numbers[idx]):
        lineStr += lines[idx][ :numbers[idx]]
        lines[idx] = lines[idx][numbers[idx]:];
      else:
        lineStr += lines[idx]
        lineFinished = True
        break
  lineStr += "\n"
  outFile.write(lineStr)
